extends Node2D
class_name BaseCell
signal on_click_event(cell)
signal on_click_entered(cell)
signal on_move_entered(cell)

@export var type_style : Array[Texture2D]

@onready var cell_model: Sprite2D = $CellModel
@onready var pos_label: Label = $PosLabel
@onready var cell_button: TextureButton = $CellModel/Cell
@onready var icon: TextureRect = $CellModel/Icon

var item_size : Vector2

var xy : Vector2i
var type : GlobelEnum.CellType = GlobelEnum.CellType.Block :
	set(v) :
		if v < 0 : return
		type = v
		_init_type_style()
var is_debug : bool
func _save_xy(x : int ,y : int) -> void:
	xy = Vector2i(x,y)

func _ready() -> void:
	cell_model.texture.width = item_size.x
	cell_model.texture.height = item_size.y
	icon.size = item_size
	icon.position = -item_size / 2.0
	cell_button.set_deferred("size",item_size)
	cell_button.position = -item_size / 2.0
	if is_debug : _set_pos_text()
	_init_type_style()
	cell_button.button_up.connect(_on_click_button_up)
	cell_button.button_down.connect(_on_click_button_down)
	cell_button.mouse_entered.connect(_on_mouse_entered)

func _set_pos_text() -> void :
	pos_label.text = str(xy)
	pos_label.show()
	cell_button.show()

func _init_type_style() -> void :
	if cell_model and type < type_style.size() :
		if not is_debug and type in [4,5] : return
		icon.texture = type_style.get(type)

func _on_click_button_up() -> void :
	if cell_button.is_hovered() :
		on_click_event.emit(self)

func _on_click_button_down() -> void :
	if cell_button.is_hovered() :
		on_click_entered.emit(self)

func _on_mouse_entered() -> void :
	on_move_entered.emit(self)

var rule : Dictionary = {
	1 : [],
	2 : ["player"],
	3 : ["sync"]
}
func _not_pass_by_role(role : String) -> bool :
	var value = rule.get(type)
	if is_same(value,null) : return false
	if value.has(role) : return false
	return true

func _check_hovered() -> bool :
	return not cell_button.is_hovered()
